/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * A text (field) (org.eclipse.swt.widgets.Text) that conforms to the SpaglCoreField interface
 * @author Sam Pottinger
 */
public class SpaglField implements spagcore.SpaglCoreField, SpaglSwtWidget {

	private static final long serialVersionUID = 1L;
	private SpaglCoreContainer parentWidget;
	private Text innerWidget; // SWT does not allow subclassing
	private SpaglCoreFont widgetFont;
	
	/**
	 * Creates a new text field using the default size and placement behaviors
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 */
	public SpaglField(SpaglCoreContainer newParent)
	{
		innerWidget = new Text(((SpaglSwtContainer)newParent).getComposite(), SWT.SINGLE | SWT.BORDER);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a new text field using a specified width and default placement behavior
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 * @param width The integer width of this text field
	 */
	public SpaglField(SpaglCoreContainer newParent, int width)
	{
		innerWidget = new Text((Composite)newParent, SWT.SINGLE | SWT.BORDER);
		innerWidget.setSize(12*width, 12);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a new text field using a specified width and default placement behavior
	 * @param newParent The SpaglContainer (or similar widget) that will hold this object
	 * @param width The integer width of this text field
	 * @param parameters The sting parameters to be used for the layout manager
	 */
	public SpaglField(SpaglCoreContainer newParent, int width, String parameters)
	{
		innerWidget = new Text(((SpaglSwtContainer)newParent).getComposite(), SWT.SINGLE | SWT.BORDER);
		innerWidget.setSize(12*width, 12);
		parentWidget = newParent;
		parentWidget.placeWidget(this, parameters);
	}
	
	/**
	 * Gets the parent container that is holding this widget
	 * @return This object's parent container as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the height in pixels of this widget
	 * 
	 * @return Gets the height
	 */
	public int getHeight() {
		return innerWidget.getSize().y;
	}

	/**
	 * Gets the width in pixels of this widget
	 * 
	 * @return The native width of this widget
	 */
	public int getWidth() {
		return innerWidget.getSize().x;
	}

	/**
	 * Gets the text displayed on the face of this widget
	 * 
	 * @return The string associated with this widget
	 */
	public String getText() {
		return innerWidget.getText();
	}

	/**
	 * Sets the text of the widget on the face of the button
	 * 
	 * @param newText the new text to be associated with this widget
	 */
	public void setText(String newText) {
		innerWidget.setText(newText);
	}

	/**
	 * Sets the size of the object in pixels
	 * 
	 * @param width the horizontal size in size x
	 * @param height the vertical size in size y
	 */
	public void setSize(int width, int height) {
		innerWidget.setSize(width, height);
	}

	/**
	 * Gets the native widget (swt only)
	 * 
	 * @return The native inner widget as a swt Text object.
	 */
	public Text getNativeWidget() {
		return (Text)innerWidget;
	}

	/**
	 * Gets the widget's font
	 * 
	 * @return The {@link SpaglFont} associated with the object as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font for this widget
	 * 
	 * @param newFont a {@link SpaglFont} to associate with the object
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
	}
	
	/**
	 * Sets the font for this widget with an option of disposing the font directly
	 * 
	 * @param newFont a {@link SpaglFont} to associate with the object
	 * @param dispose true if the font should be disposed of immediatley (not recommended)
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
		if(dispose)
		{
			widgetFont = null;
			((SpaglFont)newFont).dispose();
		}
		else
			widgetFont = newFont;
	}

	/**
	 * Determines if this widget can be typed into by the user
	 * @return True if the user can type in new text or false if he/she can not
	 */
	public boolean isEditable() {
		return innerWidget.getEnabled();
	}

	/**
	 * Sets whether this object can be typed into by the user
	 * @param newValue True if the user can type in or false if he/she can not
	 */
	public void setEditable(boolean newValue) {
		innerWidget.setEditable(newValue);
	}
	
	/**
	 * Disposes this widget through SWT
	 */
	public void dispose()
	{
		innerWidget.dispose();
	}
	
	/**
	 * Adds an event listener to this field
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param o The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object o) {
		try{
			switch(eventID)
			{
				case SpaglEvent.TEXT_CHANGED_EVENT: {innerWidget.addListener(SWT.Modify, new SpaglEventListener(o.getClass().getMethod(method, SpaglCoreEvent.class), o, SpaglEvent.SELECTION_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
}
